package com.care.service;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.care.dao.admin.AdminDao;
import com.care.dao.admin.impl.AdminDaoImpl;
import com.care.entity.Consult;
import com.care.entity.Doctor;
import com.care.entity.Knowledge;
import com.care.entity.Page;
import com.care.entity.User;

@Service
@Transactional
public class AdminServiceImpl {
	
	
	@Resource
	private AdminDaoImpl adi;
	
//	public static void main(String[] args) {
//		AdminDao ad =new AdminDaoImpl();
//		System.out.println(ad);
//	}
	
	//添加用户
	public boolean addUser(String name, String password, String sex, String email){
		User user = new User(name,password,sex,email);	
		if(adi.addUser(user)){
			return true;
		}return false;
	}
	//添加医生
	public boolean addDoctor(String name, String password, String sex, String information, double money, String section,
			String email){
		Doctor doctor = new Doctor(name,password,sex,information,money,section,email);	
		if(adi.addDoctor(doctor)){
			return true;
		}return false;
	}
	//添加医护知识

	public boolean addKnowledge(String title, String content, String doctorname){
		Doctor selDoctor = adi.selDoctor(doctorname);
		if(selDoctor!=null){
		Knowledge knowledge = new Knowledge();	
		if (adi.addKnowledge(knowledge)) {
			return true;
			
		}
		}return false;
		
	}
	
	
	
	//删除用户
	public void delUser(String name){
		User user = adi.selUser(name);
		adi.delUser(user);
	}
	//删除医生
	public void delDoctor(String name){
		Doctor doctor = adi.selDoctor(name);
		adi.delDoctor(doctor);
	}
	//删除医护知识
	public void delKnowledge(String title){
		Knowledge selKnowledge = adi.selKnowledge(title);
		adi.delKnowledge(selKnowledge);
	}
		
		
	
	//查询用户
	public User selUser(String name){
		User selUser = adi.selUser(name);
		return selUser;
	}
	//查询医生
	public Doctor selDoctor(String name){
		Doctor selDoctor = adi.selDoctor(name);
		return selDoctor;
	}
	//查询医护知识
	public Knowledge selKnowledge(String title){
		Knowledge selKnowledge = adi.selKnowledge(title);
		return selKnowledge;
	}
	
	
	
	//查询分页用户
	public Page<User> selAllUser(String number){
		int pageNum = 0;
		if(number==null || number.equals("")){
			pageNum=1;
		}else {
			pageNum=Integer.parseInt(number);	
		}
		List<User> page=adi.selAllUser(pageNum, 4);
		
		int count = adi.selCountUser();
		Page<User> p= new Page<User>(pageNum,4,count,page);
		if((pageNum-1)==0){
			p.setPageNum(pageNum=1);
		}else {
			p.setPrePageNum(pageNum-1);
		}
		if((pageNum*4)>count){
			p.setNextPageNum(pageNum);
		}else{
			p.setNextPageNum(pageNum+1);
		}
//		System.out.println(p+"123as");
		return p;
	}
	//查询分页医生
	public Page<Doctor> selAllDoctor(String number){
		int pageNum = 0;
		if(number==null || number.equals("")){
			pageNum=1;
		}else {
			pageNum=Integer.parseInt(number);	
		}
		List<Doctor> page=adi.selAllDoctor(pageNum, 4);
		
		int count = adi.selCountDoctor();
		Page<Doctor> p= new Page<Doctor>(pageNum,4,count,page);
		if((pageNum-1)==0){
			p.setPageNum(pageNum=1);
		}else {
			p.setPrePageNum(pageNum-1);
		}
		if((pageNum*4)>count){
			p.setNextPageNum(pageNum);
		}else{
			p.setNextPageNum(pageNum+1);
		}
		return p;
	}
	//查询分页医护知识
	public Page<Knowledge> selAllKnowledge(String number){
		int pageNum = 0;
		if(number==null || number.equals("")){
			pageNum=1;
		}else {
			pageNum=Integer.parseInt(number);	
		}
		List<Knowledge> page=adi.selAllKnowledge(pageNum, 4);
		
		int count = adi.selCountKnowledge();
		Page<Knowledge> p= new Page<Knowledge>(pageNum,4,count,page);
		if((pageNum-1)==0){
			p.setPageNum(pageNum=1);
		}else {
			p.setPrePageNum(pageNum-1);
		}
		if((pageNum*4)>count){
			p.setNextPageNum(pageNum);
		}else{
			p.setNextPageNum(pageNum+1);
		}
		return p;
	}
		
		
	
	//修改用户
	public boolean updUser(String name,String password, String img, String sex, double money,String email){
		boolean updUser = adi.updUser(name, password, img, sex, money, email);
		return updUser;
	}
	//修改医生
	public boolean updDoctor(String name, String password, String sex, String information, double money, String section,
			String email){
		boolean updDoctor = adi.updDoctor(name, password, sex, information, money, section, email);
		return updDoctor;
	}
	//修改医护知识
	public boolean updKnowledge(String name, String title, String content, String doctorname){
		Doctor selDoctor = adi.selDoctor(doctorname);
		if(selDoctor!=null){
		boolean updKnowledge = adi.updKnowledge(name, title, content, selDoctor);
		return updKnowledge;
		}
		return false;
	}
}
